﻿using com.yoozoo.gta.Tools.UI;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Yoozoo.Gameplay.RTS;
using Yoozoo.UI.YGUI;

public class MonsterRadarIcon : MonoBehaviour
{
    public SpriteRenderer QualityRenderer;
    public SpriteRenderer MonsterIconRenderer;
    public SpriteRenderer QualityRenderer02;
    public SpriteRenderer EquipTypeRenderer;
    public SpriteRenderer LeftUpIconRenderer;
    public SpriteRendererRadar MonsterIconRendererRadar;

    private bool _active = true;
    private bool _activeEquip = false;
    private int _radarEventQuality = 0;
    private int _radarEventType = 0;
    
    private string _monsterIcon;
    private string _rewardIcon;
    private string _equipTypeIcon;
    private string _leftUpIcon;
    
    internal void SetRaderQualityIcon(int radarEventQuality)
    {
        if (radarEventQuality != 0)
        {
            if (_radarEventQuality != radarEventQuality)
            {
                _radarEventQuality = radarEventQuality;

                ArmyManager.Instance.ResourceManager.SetRaderQualityIcon(QualityRenderer02, $"img_radar_marker_0{radarEventQuality}");

                if (_radarEventQuality == 6) 
                {
                    QualityRenderer.gameObject.SetActive(false);
                    QualityRenderer02.gameObject.SetActive(true);
                }

            }
        }
    }


    internal void SetRaderQualityIcon02(int eventType, int radarEventQuality)
    {
        if (radarEventQuality != 0 && eventType != 0)
        {
            if (_radarEventQuality != radarEventQuality || _radarEventType != eventType)
            {
                _radarEventQuality = radarEventQuality;
                _radarEventType = eventType;

                if (radarEventQuality == 6)
                {
                    QualityRenderer.gameObject.SetActive(false);
                    QualityRenderer02.gameObject.SetActive(true);
                    // ArmyManager.Instance.ResourceManager.SetRaderQualityIcon(QualityRenderer, $"{radarEventQuality}");
                    ArmyManager.Instance.ResourceManager.SetRadarBottomIconByTypeAndQuality(eventType, radarEventQuality, QualityRenderer02); 
                }
                else
                {
                    QualityRenderer.gameObject.SetActive(true);
                    QualityRenderer02.gameObject.SetActive(false);
                    ArmyManager.Instance.ResourceManager.SetRadarBottomIconByTypeAndQuality(eventType, radarEventQuality, QualityRenderer);
                    // TODO 不同设置大小
                    // QualityImage.SetNativeSize();
                }

            }
        }
    }


    internal void SetMonsterIconIcon(string monsterIcon)
    {
        if (!string.IsNullOrEmpty(monsterIcon))
        {
            if (MonsterIconRenderer.transform.gameObject.activeSelf != true)
            {
                this.MonsterIconRenderer.transform.gameObject.SetActive(true);
            }
            if (_monsterIcon != monsterIcon)
            {
                _monsterIcon = _monsterIcon;
                ArmyManager.Instance.ResourceManager.SetRaderQualityIcon(MonsterIconRenderer, monsterIcon);
                MonsterIconRenderer.transform.SetLocalPositionY(110);
                MonsterIconRenderer.transform.SetLocalScaleX(80);
                MonsterIconRenderer.transform.SetLocalScaleY(80);
            }
        }
        else
        {
            if (MonsterIconRenderer.transform.gameObject.activeSelf != false)
            {
                this.MonsterIconRenderer.transform.gameObject.SetActive(false);
            }
        }
    }

    internal void SetRadarRewardIcon(string rewardIcon)
    {
        if (!string.IsNullOrEmpty(rewardIcon))
        {
            if (MonsterIconRenderer.transform.gameObject.activeSelf != true)
            {
                this.MonsterIconRenderer.transform.gameObject.SetActive(true);
            }
            MonsterIconRenderer.transform.SetLocalPositionY(100);
            MonsterIconRenderer.transform.SetLocalScaleX(25);
            MonsterIconRenderer.transform.SetLocalScaleY(25);
            if (WebImageManager.IsFromWeb(rewardIcon))
            {
                if (_rewardIcon != rewardIcon)
                {
                    _rewardIcon = rewardIcon;
                    MonsterIconRendererRadar.spriteName = rewardIcon;
                }
            }
            else
            {
                if (_rewardIcon != rewardIcon) 
                {
                    _rewardIcon = rewardIcon;
                    ArmyManager.Instance.ResourceManager.SetRadarEventIconWithRelativePath(rewardIcon, MonsterIconRenderer);
                }
            }
        }
        else
        {
            if (MonsterIconRenderer.transform.gameObject.activeSelf != false)
            {
                this.MonsterIconRenderer.transform.gameObject.SetActive(false);
            }
        }
        
    }
    
    internal void SetRadarEquipTypeImage(string equipTypeIcon)
    {
        if (!string.IsNullOrEmpty(equipTypeIcon))
        {
            SetEquipIconActive(true);
            if (_equipTypeIcon != equipTypeIcon) 
            {
                _equipTypeIcon = equipTypeIcon;
                ArmyManager.Instance.ResourceManager.SetRadarEventIconWithRelativePath(equipTypeIcon, EquipTypeRenderer);
            }
        }
        else
        {
            SetEquipIconActive(false);
        }
    }
    
    public void SetEquipIconActive(bool active)
    {
        if (EquipTypeRenderer.transform.gameObject.activeSelf != active)
        {
            this.EquipTypeRenderer.transform.gameObject.SetActive(active);
        }
    }
    
    internal void SetRadarLeftUpIconImage(string leftUpIcon)
    {
        if (!string.IsNullOrEmpty(leftUpIcon))
        {
            SetLeftUpIconActive(true);
            if (_leftUpIcon != leftUpIcon) 
            {
                _leftUpIcon = leftUpIcon;
                ArmyManager.Instance.ResourceManager.SetHeroRtsAtlasIcon(leftUpIcon, LeftUpIconRenderer);
            }
        }
        else
        {
            SetLeftUpIconActive(false);
        }
    }
    
    public void SetLeftUpIconActive(bool active)
    {
        if (LeftUpIconRenderer.transform.gameObject.activeSelf != active)
        {
            this.LeftUpIconRenderer.transform.gameObject.SetActive(active);
        }
    }

    public void SetIconActive(bool active)
    {
        if (_active!=active)
        {
            _active = active;
            this.gameObject.SetActive(_active);
        }
    }
}
